package com.dingding.study.service.impl;

import com.dingding.study.Event.serviceImpl.DomainEventListener;
import com.dingding.study.Event.serviceImpl.DomainEventPublisher;
import com.dingding.study.entity.User;
import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.Subscribe;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @program: spring-learning
 * @description:
 * @author: liugongding
 * @create: 2021-09-13 17:35
 **/

@Component
@Slf4j
public class UserCreatedEventTransmitter implements DomainEventListener<UserCreatedEvent> {

    @Autowired
    private UserServiceImpl userService;

    @PostConstruct
    public void init() {
        DomainEventPublisher.getInstance().subscrible(this);
    }

    @Override
    @Subscribe
    @AllowConcurrentEvents
    public void handleEvent(UserCreatedEvent event) {
        userService.save(
                User.builder()
                        .username("liu2")
                        .build()
        );
        log.info("当前线程：{}", Thread.currentThread().getName());
        int a = 1/0;

    }
}
